package com.hulytu.dev2;

import android.util.ArrayMap;

import com.hulytu.dev2.plugin.EnhancePlugin;

import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public class Embrace implements IEmbrace {

    private final Map<String, EnhancePlugin> plugins = new ArrayMap<>();

    @Override
    public void onAppInit() {

    }

    @Override
    public void onDestroyed() {
        Collection<EnhancePlugin> values = plugins.values();
        for (EnhancePlugin plugin : values) {
            plugin.destroy();
        }

        plugins.clear();
    }

    @Override
    public @Nullable EnhancePlugin obtain(String alias) {
        return plugins.get(alias);
    }

    @Override
    public int register(String alias, @Nullable EnhancePlugin plugin) {
        EnhancePlugin cache = plugins.get(alias);
        if (cache != null) cache.destroy();
        plugins.put(alias, plugin);
        return cache == null ? REGISTER_SUCCESS : REGISTER_UPDATE;
    }

    @Override
    public EnhancePlugin[] plugins() {
        return plugins.values().toArray(new EnhancePlugin[0]);
    }
}
